package airplane.com.example.controller;

import airplane.com.example.entry.User;
import airplane.com.example.service.UserService;
import airplane.com.example.utilities.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/findByPage")
    public Map<String,Object> findByPage(Integer currentPage, Integer pageSize) {
        currentPage = currentPage == null ? 0 : currentPage;
        pageSize = pageSize == null ? 14 : pageSize;
        Map<String, Object> map = new HashMap<>();
        map.put("user", userService.findByPage(currentPage, pageSize));
        map.put("pageTotal", userService.findTotal());
        return map;
    }

    @GetMapping("/findAll")
    public Result findAll(User user) {
        return userService.queryUserList(user);
    }

    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user) {
        return userService.register(user);
    }

    @PostMapping("/deleteUser")
    public Result deleteUser(@RequestBody User user) {
        return userService.deleteUser(user);
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        try {
            return userService.login(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("系统错误：登录失败,请重试！", false);
        }
    }
}
